/**
 * Copyright &copy; 2012-2016 civet All rights reserved.
 */
package cn.mcsys.myadmin.modules.air.web;

import cn.mcsys.myadmin.common.config.Global;
import cn.mcsys.myadmin.common.persistence.Page;
import cn.mcsys.myadmin.common.web.BaseController;
import cn.mcsys.myadmin.common.web.JsonResult;
import cn.mcsys.myadmin.modules.air.entity.AirParkingLot;
import cn.mcsys.myadmin.modules.air.entity.AirRidePoint;
import cn.mcsys.myadmin.modules.air.service.AirParkingLotService;
import cn.mcsys.myadmin.modules.air.service.AirRidePointService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 乘车点管理Controller
 * @author leopold
 * @version 2018-08-13
 */
@Controller
@RequestMapping(value = "${adminPath}/air/airRidePoint")
public class AirRidePointController extends BaseController {

	@Autowired
	private AirRidePointService airRidePointService;

	@Autowired
	private AirParkingLotService parkingLotService;
	
	@RequiresPermissions("air:airRidePoint:view")
	@RequestMapping(value = {"list", ""})
	public String list(AirRidePoint airRidePoint, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<AirRidePoint> page = airRidePointService.findPage(new Page<AirRidePoint>(request, response), airRidePoint); 
		model.addAttribute("page", page);
		return "modules/air/airRidePointList";
	}

	@RequiresPermissions("air:airRidePoint:view")
	@RequestMapping(value = "form")
	public String form(AirRidePoint airRidePoint, Model model) {
	    if (airRidePoint.getId() != null){
            airRidePoint = airRidePointService.get(airRidePoint.getId());
        }
		List<AirParkingLot> parkingLots = parkingLotService.findAllList(new AirParkingLot());
		model.addAttribute("airRidePoint", airRidePoint);
		model.addAttribute("parkingLots",parkingLots);
		return "modules/air/airRidePointForm";
	}

	@RequiresPermissions("air:airRidePoint:edit")
	@RequestMapping(value = "save")
	public String save(AirRidePoint airRidePoint, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, airRidePoint)){
			return form(airRidePoint, model);
		}
		airRidePointService.save(airRidePoint);
//		addMessage(redirectAttributes, "保存乘车点成功");
		return "redirect:"+Global.getAdminPath()+"/air/airRidePoint/?repage";
	}
	
	@RequiresPermissions("air:airRidePoint:edit")
	@RequestMapping(value = "delete")
	public String delete(AirRidePoint airRidePoint, RedirectAttributes redirectAttributes) {
		airRidePointService.delete(airRidePoint);
//		addMessage(redirectAttributes, "删除乘车点成功");
		return "redirect:"+Global.getAdminPath()+"/air/airRidePoint/?repage";
	}

	@RequestMapping(value = "getById")
	@ResponseBody
	public JsonResult getById(Integer id) {
		return JsonResult.success(airRidePointService.get(id));
	}

	@RequestMapping(value = "getCountById")
	@ResponseBody
	public JsonResult getCountById(Integer id) {
		return JsonResult.success(airRidePointService.getCountById(id));
	}

	@RequestMapping(value = "getSeatById")
	@ResponseBody
	public JsonResult getSeatById(Integer id) {
		return JsonResult.success(airRidePointService.getSeatById(id));
	}

	@RequestMapping(value = "RouteIsBind")
	@ResponseBody
	public JsonResult RouteIsBind(Integer id,Integer route) {
		return JsonResult.success(airRidePointService.RouteIsBind(id,route));
	}
}